import { store } from "@/store"
import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { showError } from '@/utils/msg-show'
import { getPageApi } from '@/api/anchor/list'
import { getPageApi as getMaterPageApi } from '@/api/sys/organization'

export const useAnchorListStore = defineStore("AnchorList", () => {

	const isZero = ref(true)
	const dataList = ref<any[]>([])
	const masterDataList = ref<any[]>([])
	const masterQuery = reactive<any>({
		pageNum: 1,
		pageSize: 100,
		totalSize: 0,
		keyword: '',
	})
	const query = reactive<any>({
		pageNum: 1,
		pageSize: 10,
		totalSize: 0,
		nickename: '',
        userId: '',
        national: '',
		date: '',
		phone: '',
		masterId:'',
	})
    
    /** 分页获取主播个人视频 */
	async function getPage() {
		await getPageApi(query).then((res: any) => {
			query.totalSize = res.data.total
			if(query.pageNum == 1){
				dataList.value = res.data.list
			}else{
				dataList.value = dataList.value.concat(res.data.list)
			}
		}).catch(err => {
			showError(err.msg || err.errMsg || err)
		})
	}

    /** 分页获取代理商 */
    function getMasterPage(){
		getMaterPageApi(masterQuery).then((res: any) => {
			masterQuery.totalSize = res.data.total
			if(masterQuery.pageNum == 1){
				masterDataList.value = res.data.list
			}else{
				masterDataList.value = masterDataList.value.concat(res.data.list)
			}
		}).catch(err => {
			showError(err.msg || err.errMsg || err)
		})
	}


	/** 初始化 */
	async function init() {
		if (isZero.value) {
            await getPage()
			getMasterPage()
			isZero.value = false
		}
	}

	/** 更新 */
    function update(){
		query.pageNum = 1
		getPage()
	}

	return {
		init, update, getPage, getMasterPage,
		query, dataList, masterDataList, masterQuery
	}

})

export function useAnchorListStoreHook() {
	return useAnchorListStore(store);
}